package com.lobstar.xrpc.common.po;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lobstar.xrpc.service.utils.JsonUtils;

public class SMessage {

	private String id;//用来标识message的request和response
	private String method;
	private Map<String, Object> values;
	private Map<String, Object> meta;
	private Type type;

	public SMessage() {
		this.values = new HashMap<String, Object>();
		this.meta = new HashMap<String, Object>();
		this.id = UUID.randomUUID().toString();
	}

	public SMessage addAttribute(String key, Object value) {
		values.put(key, value);
		return this;
	}
	public SMessage addAttributes(Map<String,Object> args) {
		if(args != null) {
			values.putAll(args);
		}
		return this;
	}
	public Map<String,Object> getParams() {
		return values;
	}
	public SMessage buildRequest() {
		this.type = Type.REQUEST;
		return this;
	}
	public SMessage buildResponse() {
		this.type = Type.RESPONSE;
		return this;
	}

	public SMessage addMeda(String key, String value) {
		meta.put(key, value);
		return this;
	}

	public SMessage type(Type type) {
		this.type = type;
		return this;
	}

	public SMessage method(String method) {
		this.method = method;
		return this;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public Map<String, Object> getMeta() {
		return meta;
	}

	public void setMeta(Map<String, Object> meta) {
		this.meta = meta;
	}

	public enum Type {
		INIT,HEART, CALL,REQUEST,RESPONSE
	}

	@Override
	public String toString() {
		try {
			return JsonUtils.toJson(this);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String getId() {
		return id;
	}
	protected void setId(String id) {
		this.id = id;
	}
}
